package com.pk.platform.business.data.service.impl;

import java.io.File;
import java.util.List;

import com.pk.platform.business.data.service.IBackupService;
import com.pk.platform.business.data.service.IBaseDataService;
import com.pk.platform.domain.data.BaseData;
import com.pk.platform.util.DateConverter;
import com.pk.platform.util.FileUtil;
import com.pk.platform.util.PropertiesUtil;
import com.pk.platform.util.ZipUtil;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.thread.StreamGobbler;

public class BackupServiceImpl implements IBackupService {

	private IBaseDataService baseDataService;
	
	/**
	 * <description> 数据备份
	目录按当前年月日，如：f:/backup/2011/11/18.sql
	backup.properties里的值为：(1)调用的bat文件路径，(2)备份到哪个目录
	从基础数据backupCommand获得备份命令，再从jdbc.properties获得数据库用户名与密码，替换备份命令
	写入bat文件
	执行bat文件
	压缩文件
	*/
	public synchronized String backupData()
	{
		try
		{
			PropertiesUtil pu = new PropertiesUtil(Constant.BACKUP_PROPERTIES);
			String batPath = pu.getValue("batPath");
			String backupPath = pu.getValue("backupPath");
			String year = DateConverter.getCurrentYearStr();
			String month = DateConverter.getCurrentMonthStr();
			String day = DateConverter.getCurrentDayStr();
			StringBuffer resultPath = new StringBuffer();
			resultPath.append(backupPath).append(year).append("/").append(month).append("/");
			File file = new File(resultPath.toString());
			FileUtil.makeDir(file);
			StringBuffer sqlBackupPath = new StringBuffer(resultPath.toString());
			resultPath.append(day).append(".sql");
			
			//写bat文件，导出数据
			this.writeBat(batPath, resultPath.toString());
			Process p = Runtime.getRuntime().exec(batPath);
			
			//线程阻塞
			StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");          
			errorGobbler.start();
			StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");
			outGobbler.start();
			p.waitFor();
			
			//压缩
			String zipPath = sqlBackupPath.append(day).append(".zip").toString();
			ZipUtil.zip(resultPath.toString(), zipPath);
			
			//删除原来的文件
			FileUtil.delFile(resultPath.toString());
			return zipPath;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	private void writeBat(String batPath, String backupPath)
	{
		List<BaseData> commandList = baseDataService.queryBaseDataByParentId("backupCommand");
		FileUtil fu = new FileUtil(batPath);
		fu.clean();
		int len = commandList.size();
		for(int i = 0; i < len; i++)
		{
			BaseData bdx = commandList.get(i);
			String command = bdx.getDataValue();
			if("command_backup_handle".equals(bdx.getId())) //替换用户，密码，路径，这样是以防泄漏给外界
			{
				PropertiesUtil pu = new PropertiesUtil(Constant.JDBC_PROPERTIES);
				command = command.replace("#user#", pu.getValue("jdbc.username"));
				command = command.replace("#pass#", pu.getValue("jdbc.password"));
				command = command.replace("#path#", backupPath);
			}
			fu.appendLine(command);
		}
	}

	public IBaseDataService getBaseDataService() {
		return baseDataService;
	}

	public void setBaseDataService(IBaseDataService baseDataService) {
		this.baseDataService = baseDataService;
	}

}
